﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
using System.Reflection;

namespace ASmile.Web
{
    public class SwaggerOperationFilter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
            {
                operation.Parameters = new List<OpenApiParameter>();
            }

            UserAuthorizeAttribute authAttr = context.MethodInfo.GetCustomAttribute<UserAuthorizeAttribute>();
            if (authAttr == null)
            {
                authAttr = context.MethodInfo.ReflectedType.GetCustomAttribute<UserAuthorizeAttribute>();
            }

            if (authAttr?.IsAuth == true)
            {
                operation.Parameters.Add(new OpenApiParameter
                {
                    Name = Config.THIS.TokenName,
                    In = ParameterLocation.Header,
                    Required = true,
                    Description = "Token 字符串",
                    AllowEmptyValue = false
                });
            }

            //BodyEncryptFilterAttribute enAttr = context.MethodInfo.GetCustomAttribute<BodyEncryptFilterAttribute>();
            //if (authAttr == null)
            //{
            //    enAttr = context.MethodInfo.ReflectedType.GetCustomAttribute<BodyEncryptFilterAttribute>();
            //}
            //if (enAttr?.IsEncrypt == true)
            //{
            //    operation.Parameters.Add(new OpenApiParameter
            //    {
            //        Name = CommonConfig.THIS.BodyEncryptKeyName,
            //        In = ParameterLocation.Header,
            //        Required = true,
            //        Description = "body加密密钥",
            //        AllowEmptyValue = true
            //    });
            //}

        }

    }
}
